package pl.kosmowska.ewelina.server.jdo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;

import pl.kosmowska.ewelina.server.dao.UserDataDAO;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
@Version(strategy=VersionStrategy.VERSION_NUMBER, column="version", extensions={@Extension(vendorName="datanucleus", key="key", value="version")})
public class CategoryData implements DatabaseEntity<String>{

	  @PrimaryKey
	  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	  private Key key;


	  @Persistent
	  private Long version;


	  @Persistent
	  private Long userId; 


	  @Persistent
	  private String categoryName;


	  @Persistent(defaultFetchGroup = "true", dependentElement = "true")  
	  private List<CategoryItemData> items;
	  
	  @Persistent
	  private boolean defaultCategory;
	  

	  public void setId(String id) {
	    if (id == null) {
	      return;
	    }
	    key = KeyFactory.stringToKey(id);
	  }
	  public String getId() {
	    String id = null;
	    if (key != null) {
	      id = KeyFactory.keyToString(key);
	    }
	    return id;
	  }

	  public void setVersion(Long version) {
	    this.version = version;
	  }
	  public Long getVersion() {
	    if (version == null) {
	      version = 0l;
	    }
	    return version;
	  }

	  public void setUserId(Long userId) {
	    this.userId = userId;
	  }
	  public Long getUserId() {
	    return userId;
	  }

	  public void setName(String name) {
	    this.categoryName = name;
	  }
	  public String getName() {
	    return categoryName;
	  }

	  public void setItems(List<CategoryItemData> items) {
	    if (items == null) {
	      this.items = null;
	      return;
	    }

	    // set owner, so the owner can remove later
	    Long uid = UserDataDAO.getLoggedInUserId();

	    Iterator<CategoryItemData> itr = items.iterator();
	    if (itr == null) {
	      this.items = null;
	      return;
	    }
	    ArrayList<CategoryItemData> a = new ArrayList<CategoryItemData>();
	    while(itr.hasNext()) {
	    	CategoryItemData d = itr.next();
	      if (d != null && d.getId() != null) {
	        d.setId(key, d.getId());
	      }
	      if (d != null) {
	        d.setUserId(uid);
	        a.add(d);
	      }
	    }
	    this.items = items;
	  }
	  
	  public List<CategoryItemData> getItems() {
	    return items;
	  }
	public boolean isDefaultCategory() {
		return defaultCategory;
	}
	
	public void setDefaultCategory(boolean defaultCategory) {
		this.defaultCategory = defaultCategory;
	}

	
	 
}

